<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable @clear="goodsList">
            <el-button slot="append" icon="el-icon-search" @click="goodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="Productlists" border stripe>
              <el-table-column type="index"></el-table-column>
              <el-table-column label="商品名称" prop="goods_name"></el-table-column>
              <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
              <el-table-column label="商品重量" prop="goods_weight" width="75px"></el-table-column>
              <el-table-column label="创建时间" prop="add_time" width="150px">
                <template slot-scope="scope">
                   {{scope.row.add_time | dateFormat}}
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini">
                        编辑
                    </el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteitem(scope.row.goods_id)">
                        删除
                    </el-button>
                </template>
              </el-table-column>
      </el-table>
      <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.pagenum"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="queryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数对象
        queryinfo:{
            query:'',
            pagenum: 1,
            pagesize: 10
        },
        // 商品列表数据
        Productlists:[],
        // 总页数
        total:0,
        // 添加商品对话框显示与隐藏
        addinggoods: false,
        // 添加商品的表单数据
        addinggoodsForm: {
            goods_name: '',
            goods_cat: '',
            goods_price: '',
            goods_number: '',
            goods_weight: '',
        },
        // 添加商品的验证规则对象
         addinggoodsFormRules:{
        goods_name: [
          {required:true,message:'请输入商品名',trigger:'blur'}
        ],
         goods_price: [
          {required:true,message:'请输入价格',trigger:'blur'}
        ],
         goods_number: [
          {required:true,message:'请输入数量',trigger:'blur'}
        ],
         goods_weight: [
          {required:true,message:'请输入重量',trigger:'blur'}
        ]
      },
    }
  },
  created () {
      this.goodsList()      
  },
  methods: {
     async goodsList () {
        const {data: res} = await this.$http.get('goods',{params:this.queryinfo})
        if (res.meta.status !== 200) {
            return this.$message.error('获取商品列表失败!')
        }
        // console.log(res);
        this.Productlists = res.data.goods
        this.total = res.data.total
      },
       // 监听pagesize改变的事件
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryinfo.pagesize = newSize
      this.goodsList()
    },
    //监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryinfo.pagenum = newPage
     this.goodsList()
    },
    // 删除商品
    async deleteitem (id) {
      // console.log(id)
       const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        }).catch(err => err)
      // console.log(confirmResult);
      if(confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      
      const {data: res} = await this.$http.delete('goods/' + id)
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败!')
      }
      this.$message.success('删除商品成功!')
      this.goodsList()
    },
    goAddpage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>